//
//  MenuLayer.m
//  SCRAP_ObjC
//
//  Created by Cezara Coroianu on 11/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MenuLayer.h"


@implementation MenuLayer

static NSString * playerName = nil;
static BOOL queuedToPlay = false;
static BOOL hasEnteredName = false;

+(id) scene
{
	MenuLayer *scene = [MenuLayer node];
	return scene;
}


-(id) init{
	if((self =[super init]))
	{
		gsession = [[[iPhoneSession alloc] init] retain];
		//[[CCDirector sharedDirector] setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
		size = [[CCDirector sharedDirector] winSize];
		center = ccp(size.width/2, size.height/2);
		
		CCSprite *background = [CCSprite spriteWithFile:@"new_menu.png"];
		background.position = center;
		[self addChild:background];
	
		
		//menu items		
		CCSprite *normalButton = [CCSprite spriteWithFile:@"purple.png"];
		CCSprite *selectButton = [CCSprite spriteWithFile:@"purple_select.png"];
		
		CCSprite *helpButton = [CCSprite spriteWithFile:@"yellow.png"];
		CCSprite *selectHelp = [CCSprite spriteWithFile:@"yellow_select.png"];
		
		CCSprite *changeButton = [CCSprite spriteWithFile:@"light_blue.png"];
		CCSprite *selectName = [CCSprite spriteWithFile:@"light_blue_select.png"];
		
		
		CCMenuItemSprite *startbt = [CCMenuItemSprite itemFromNormalSprite:normalButton selectedSprite:selectButton target:self selector:@selector(newGame)];
		
		CCMenuItemSprite *helpbt = [CCMenuItemSprite itemFromNormalSprite:helpButton selectedSprite:selectHelp target:self selector:@selector(instrMenu)];
		
		CCMenuItemSprite *changebt = [CCMenuItemSprite itemFromNormalSprite:changeButton selectedSprite:selectName target:self selector:@selector(changeName)];

		CCMenu *menu = [CCMenu menuWithItems:startbt,helpbt,changebt, nil];
		menu.position = ccp(center.x, 60);
		[menu alignItemsHorizontally];
		
		if (playerName == nil) {
			NSString * nameInput = [[UIDevice currentDevice] name];
			int length = [nameInput length] > 15 ? 15 : [nameInput length];
			
			playerName = [[nameInput substringToIndex:length] retain];
			gsession.playerName = playerName;
		}
		
		welcomeLabel = [[CCLabel labelWithString:[NSString stringWithFormat:PlayerWelcome, playerName]
									   fontName:SCRAPFont
									   fontSize:20]
						retain];
		welcomeLabel.position = ccp(center.x, 140);
		
		[self addChild:menu];
		[self addChild:welcomeLabel];
		 
	}
	return self;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	//1 is OK, 0 is Cancel R
	
	UITextField *tf = [alertView textFieldAtIndex:0];
	NSString * nameInput = [tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	
	if (buttonIndex == 1 && ![nameInput isEqualToString:@""]) {
		[playerName release];
		int length = [nameInput length] > 15 ? 15 : [nameInput length];
		
		playerName = [[nameInput substringToIndex:length] retain];
		gsession.playerName = playerName;
		[welcomeLabel setString:[NSString stringWithFormat:PlayerWelcome, playerName]];
		hasEnteredName = true;
		
		if (queuedToPlay) {
			queuedToPlay=false;
			[self newGame];
		}
	}
	else {
		queuedToPlay = false;
	}
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
	//label.text = textField.text;
	[textField resignFirstResponder];
	
	[textField endEditing:true];
	
    return YES;
}

-(void) newGame{
	if (!hasEnteredName) {
		queuedToPlay=true;
		[self changeName];
		return;
	}
	gsession.playerName = playerName;
	
	//[[CCDirector sharedDirector] setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
	
	BackgroundLayer * bl = [BackgroundLayer node];
	[bl setPicker:gsession];
	
	[[CCDirector sharedDirector] replaceScene:[CCFadeTRTransition transitionWithDuration:1 scene:bl]];
	//[[CCDirector sharedDirector] replaceScene:bl];

}

-(void)instrMenu{
	[[CCDirector sharedDirector] replaceScene:[CCFadeTRTransition transitionWithDuration:.5 scene:[InstructionsLayer node]]];
	//[[CCDirector sharedDirector] replaceScene:[InstructionsLayer node]];
	
}
-(void)changeName{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SCRAP" message:@"Enter your name" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
	[alert addTextFieldWithValue: playerName label:@"Enter your name"];
		
	//name field
	UITextField *tf = [alert textFieldAtIndex:0];
	tf.text = playerName;
	tf.returnKeyType = UIReturnKeyDone;
	tf.clearButtonMode = UITextFieldViewModeWhileEditing;
	tf.keyboardType = UIKeyboardTypeAlphabet;
	tf.keyboardAppearance = UIKeyboardAppearanceAlert;
	tf.autocapitalizationType= UITextAutocapitalizationTypeWords;
	tf.autocorrectionType = UITextAutocorrectionTypeNo;
	
	[alert show];
}

-(void) dealloc
{
	[gsession release];
	[super dealloc];
}

@end
